﻿using DevExpress.Data.Filtering;
using Volo.Abp.DependencyInjection;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.TabPanel.Queue;

public class AdvancedSearchExpression : WorkflowBase, IAdvancedSearchExpression, ITransientDependency
{
    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        const string key = "Input";
        CriteriaOperator criteriaOperator = CriteriaOperator.FromLambda<BusinessObjects.Exams.Exam>(e => true);
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is Dictionary<string, object> search)
        {
            //返回CriteriaOperator
            //检查号
            var accessionNumber = search.GetValueOrDefault("accessionNumber") as string;
            if (!string.IsNullOrWhiteSpace(accessionNumber))
            {
                var criteria = CriteriaOperator.FromLambda<BusinessObjects.Exams.Exam>(e => e.AccessionNumber == accessionNumber);
                criteriaOperator = CriteriaOperator.And(criteriaOperator, criteria);
            }
            //患者姓名
            var name = search.GetValueOrDefault("patientName") as string;
            if (!string.IsNullOrWhiteSpace(name))
            {
                var criteria = CriteriaOperator.FromLambda<BusinessObjects.Exams.Exam>(e => e.Patient != null && e.Patient.Name == name);
                criteriaOperator = CriteriaOperator.And(criteriaOperator, criteria);
            }
            //设备
            var device = search.GetValueOrDefault("device") as Guid?;
            if (device != null && device.Value != Guid.Empty)
            {
                var criteria = CriteriaOperator.FromLambda<BusinessObjects.Exams.Exam>(e => e.ExamDevice != null && e.ExamDevice.Id == device);
                criteriaOperator = CriteriaOperator.And(criteriaOperator, criteria);
            }
            //设备类型
            var deviceType = search.GetValueOrDefault("deviceType") as Guid?;
            if (deviceType != null && deviceType.Value != Guid.Empty)
            {
                var criteria = CriteriaOperator.FromLambda<BusinessObjects.Exams.Exam>(e => e.DeviceType != null && e.DeviceType.Id == deviceType);
                criteriaOperator = CriteriaOperator.And(criteriaOperator, criteria);
            }
        }
        input.Add("Output", criteriaOperator);
        return await Task.FromResult(new WorkflowOutput
        {
            Success = true
        });
    }
}
